package com.kaifamiao.high.thread;

import java.time.LocalTime;

/**
 * @author lucien-ma
 * @date 2021/10/9
 * @apiNote
 */
public class Demo10 {
    public static void main(String[] args) {
        TimeThread timeThread = new TimeThread();
        // 设置为守护线程
        timeThread.setDaemon(true);
        timeThread.start();

        // 启动其他接口
        new Thread(new MyRunnable(5000), "线程A").start();
        new Thread(new MyRunnable(4000), "线程B").start();
    }
}

/**
 * 时间线程
 */
class TimeThread extends Thread {

    @Override
    public void run() {
        while (true) {
            System.out.println("当前时间是：" + LocalTime.now());
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyRunnable implements Runnable {
    private long sleepTime;

    public MyRunnable(long sleepTime) {
        this.sleepTime = sleepTime;
    }

    @Override
    public void run() {
        System.err.println("线程：" + Thread.currentThread().getName() + "准备休眠:" + sleepTime + "毫秒");
        try {
            Thread.currentThread().sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
